export default class CookieManager {
    /**
     * 获取 cookie
     * @param key cookie 名称 / 键名
     */
    static get(key: string): string | number | undefined {
        const name = key + "=";
        const ca = document.cookie.split(";");
        for (let i = 0; i < ca.length; i++) {
            const c = ca[i].trim();
            if (c.indexOf(name) == 0) {
                return c.substring(name.length, c.length);
            }
        }
        return undefined;
    }

    /**
     * 设置 cookie
     * @param key cookie 名称 / 键名
     * @param value cookie 值
     * @param exp cookie 到期时间戳
     */
    static set(key: string, value: string | number, exp: number) {
        const expires = "expires=" + new Date(exp).toUTCString();
        return (document.cookie = key + "=" + value + "; path=/; " + expires);
    }

    /**
     * 删除 cookie
     * @param key cookie 名称 / 键名
     */
    static delete(key: string) {
        return (document.cookie = `${key}=; expires=Thu, 01 Jan 1970 00:00:00 GMT`);
    }
}